package com.tsoj.Interceptor;


import com.tsoj.common.ErrorCode;
import com.tsoj.common.ResultUtils;
import com.tsoj.context.ThreadLocalContext;
import com.tsoj.propertise.JwtPropertise;
import com.tsoj.utils.JwtUtils;
import com.tsoj.utils.RedisUtils;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * jwt拦截器
 */
@Component
@Slf4j
public class JwtInterceptor implements HandlerInterceptor {
    @Autowired
    private JwtUtils jwtUtils;
    @Autowired
    private JwtPropertise jwtPropertise;
    @Autowired
    private RedisUtils redisUtils;
    //拦截登陆前的请求
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if (!(handler instanceof HandlerMethod)) {
            //当前拦截的静态方法放行
            return true;
        }
        String token = request.getHeader(jwtPropertise.getUserTokenName());
        try {
            //校验令牌
            log.info("jwt校验");
            Claims claims = JwtUtils.parseJwt(jwtPropertise.getUserSecretKey(), token);
            Long userId = Long.valueOf(claims.get("userId").toString());
            //进行redis校验
            
         
            System.out.println(userId);
            //将用户id保存到ThreadLocal中
            ThreadLocalContext.setUserId(userId);
            log.info("jwt校验通过");
            return true;
        } catch (Exception e) {
            ResultUtils.error(ErrorCode.NOT_LOGIN_ERROR);
            return false;
        }
    }
    //第二版
    public boolean preHandle1(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if (!(handler instanceof HandlerMethod)) {
            //当前拦截的静态方法放行
            return true;
        }
        String token = request.getHeader(jwtPropertise.getUserTokenName());
        try {
            //校验令牌
            log.info("jwt校验");
            Claims claims = JwtUtils.parseJwt(jwtPropertise.getUserSecretKey(), token);
            Long userId = Long.valueOf(claims.get("userId").toString());
            //进行redis校验
            boolean isHave = redisUtils.hasKey(token);
            if(!isHave){
                //登陆过期 返回状态码
                ResultUtils.error(ErrorCode.NOT_LOGIN_ERROR);
            }

            System.out.println(userId);
            //将用户id保存到ThreadLocal中
            ThreadLocalContext.setUserId(userId);
            log.info("jwt校验通过");
            return true;
        } catch (Exception e) {
            ResultUtils.error(ErrorCode.NOT_LOGIN_ERROR);
            return false;
        }
    }
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        // 清理 ThreadLocal
        ThreadLocalContext.remove();
    }
}
